import {
  CommentsQueryType,
  getCommentsByIdAPI,
  sendCommentAPI,
  SendCommentParamsType,
} from '@/api/comments';
import { AppDispatch } from '../reducer';
import { CommentsActionType, CommentsType } from '../reducer/comments';

/** 获取文章评论Action */
export function getCommentsByIdAction(params: CommentsQueryType) {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    console.log(res);
    dispatch({ type: 'comments/saveComments', payload: res.data });
  };
}

/** 清除文章评论Action */
export function ClearCommentsAction(): CommentsActionType {
  return { type: 'comments/clearComments' };
}

/**保存原始评论 */
export function saveOriginCommentAction(
  comment: CommentsType
): CommentsActionType {
  return { type: 'comments/saveOriginComment', payload: comment };
}

/**清除原始评论 */
export function ClearOriginCommentAction(): CommentsActionType {
  return { type: 'comments/clearOriginComment' };
}

/** 发表文章评论Action */
export function sendCommentAction(params: SendCommentParamsType) {
  return async (dispacth: AppDispatch) => {
    const res = await sendCommentAPI(params);
    console.log(res);
  };
}

/** 获取回复评论列表Action */
export function getReplyCommentsByIdAction(params: CommentsQueryType) {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    console.log(res);
    dispatch({ type: 'comments/saveReplyComments', payload: res.data });
  };
}

/** 清除回复评论列表Action */
export function ClearReplyCommentsAction(): CommentsActionType {
  return { type: 'comments/clearReplyComments' };
}

/** 更新回复评论数 */
export function updateReplyCommentCountAction(
  reply_count: number
): CommentsActionType {
  return { type: 'comments/updateReplyCommentCount', payload: reply_count };
}
